package com.sugar.real.estate.module.business.dal.mysql.openedcity;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sugar.real.estate.framework.mybatis.core.mapper.BaseMapperX;
import com.sugar.real.estate.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.sugar.real.estate.module.business.controller.admin.openedcity.vo.OpenedCityListReqVO;
import com.sugar.real.estate.module.business.dal.dataobject.openedcity.OpenedCityDO;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

/**
 * 已开通城市 Mapper
 *
 * @author Ysugar
 */
@Mapper
public interface OpenedCityMapper extends BaseMapperX<OpenedCityDO> {

    default OpenedCityDO selectByParentIdAndNameZhCn(Long parentId, String nameZhCn, String nameEnUs) {
        return selectOne(Wrappers.<OpenedCityDO>lambdaQuery()
                .eq(OpenedCityDO::getParentId, parentId)
                .nested(o -> o.eq(OpenedCityDO::getNameZhCn, nameZhCn).or().eq(OpenedCityDO::getNameEnUs, nameEnUs)));
    }

    default List<OpenedCityDO> selectList(OpenedCityListReqVO reqVO) {
        String name = reqVO.getNameZhCn();
        return selectList(new LambdaQueryWrapperX<OpenedCityDO>()
                .eqIfPresent(OpenedCityDO::getParentId, reqVO.getParentId())
                .eqIfPresent(OpenedCityDO::getType, reqVO.getType())
                .betweenIfPresent(OpenedCityDO::getCreateTime, reqVO.getCreateTime())
                .nested(StrUtil.isNotBlank(name),
                        o -> o.like(OpenedCityDO::getNameZhCn, name).or().like(OpenedCityDO::getNameEnUs, name))
                .orderByDesc(OpenedCityDO::getId));
    }

    default Long selectCountByParentId(Long parentId) {
        return selectCount(OpenedCityDO::getParentId, parentId);
    }

}